<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 品牌故事列表 控制层
 * @author adela.xu 8.29
 */
class Material extends My_Controller
{
    private $assign = array();
    /**
     * 默认方法
     * @author Toby.tu 2016-09-01
     */
    public function init(){
        $this->vendor('models/Material_model','material_model');
        $this->assign['module'] = 'material';
        $this->assign['is_admin'] = $this->permis_logic->isAdmin();
    }
    /**
     * 用户列表
     * @author Toby.tu 2016-08-30
     */
    public function index(){
        $input = $this->input->post();
        $cur_page = 1;
        if(isset($input['cur_page'])){
            $cur_page = $input['cur_page'];
        }
        $search = array();
        $filter = array(
            'name','status'
        );
        foreach ($filter as $fld) {
            if(isset($input[$fld]) && trim($input[$fld]) != ''){
                $search[$fld] = $input[$fld];
            }
        }
        $brand_id = 0;
        if(isset($input['brand_id']) && $input['brand_id'] > 0){
            $brand_id = $input['brand_id'];
        }else if('yes' != $this->assign['is_admin']){//非管理员
            $brand_id = $this->permis_logic->getLoginUserInfo()['brand_id'];
            if(empty($brand_id)){
                die('没有品牌权限。');
            }
        }
        if($brand_id > 0) $search['brand_id'] = $brand_id;
        $search['is_admin'] = $this->assign['is_admin'];//是否管理员
        //类型
        $type = 0;
        if(isset($input['type']) && $input['type'] > 0){
            $type = $input['type'];
        }
        $search['type'] = $type;//类型
        // $this->pagesize = 2;
        $materialList = $this->material_model->getMaterialList($search,$cur_page,$this->pagesize);
        $page_count = $this->material_model->getMaterialCount($search);
        $this->assign['materialList'] = $materialList;
        $pagetitle = '';$paginate = '';
        if($page_count > 0){
            $paginate = $this->paginate($cur_page,$page_count);
            $start = (($cur_page - 1) * $this->pagesize ) + 1;
            $end = ($start+$this->pagesize-1);
            if($end > $page_count){
                $end = $page_count;
            }
            $pagetitle = $start.' ~ '.$end;
            $pagetitle .= ' &nbsp;&nbsp;&nbsp;&nbsp;共 '.$page_count.' 条';
        }
        $this->assign['paginate'] = $paginate;
        $this->assign['pagetitle'] = $pagetitle;
        $this->assign['search'] = $search;
        $this->assign['cur_page'] = $cur_page;
        //获取是否管理员选项
        $this->vendor('@.Picklist_model');
        $is_start_opts = $this->picklist_model->getOptsByName('is_start');
        $this->assign['is_start_opts'] = $is_start_opts;
        $this->assign['is_start_label'] = current($is_start_opts)['label'];

        $this->vendor('@.Brandinfo_model');
        $brandinfos = $this->brandinfo_model->getBrandinfoByBrandId($brand_id);
        $this->assign['brandinfo_opts'] = $brandinfos;
        //获取品牌列表
        if('yes' == $this->assign['is_admin']){
            $this->vendor('@.Brand_model');
            $brands = $this->brand_model->getAll();
            $this->assign['brand_opts'] = $brands;
        }
        if(isset($input['ajax']) && 1 == $input['ajax']){
            $this->load->view('admin/material/list',$this->assign);
        }else{
            $this->assign['view'] = 'admin/material/index';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function create(){
        $this->vendor('@.Picklist_model');
        $is_start_opts = $this->picklist_model->getOptsByName('is_start');
        $this->assign['is_start_opts'] = $is_start_opts;
        $this->assign['is_start_label'] = current($is_start_opts)['label'];
        //获取品牌列表
        if('yes' == $this->assign['is_admin']){
            $this->vendor('@.Brand_model');
            $brands = $this->brand_model->getAll();
            $this->assign['brand_opts'] = $brands;
        }
        $input = $this->input->get();
        $this->assign['material_type'] = (isset($input['material_type'])?$input['material_type']:0);
        if(0 == $this->assign['material_type']){
            $this->assign['material_title'] = '图片';
        }else{
            $this->assign['material_title'] = '视频';
        }
        $this->assign['view'] = 'admin/material/create';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function store(){
        try {
            $input = $this->input->post();
            // dd($input);
            if(0 == $input['material']['material_type']){
                $material_title = '图片';
            }else{
                $material_title = '视频';
            }
            if(!isset($input['material'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['material']['name'])){
                throw new Exception('请输入'.$material_title.'名称。');
            }
            $brand_id = 0;
            if(isset($input['material']['brand_id']) && $input['material']['brand_id'] > 0){
                $brand_id = $input['material']['brand_id'];
            }else if('yes' != $this->assign['is_admin']){//非管理员
                $brand_id = $this->permis_logic->getLoginUserInfo()['brand_id'];
            }
            if(empty($brand_id)){
                throw new Exception('没有对应的品牌，不能保存。');
            }
            $where = ['name'=>$input['material']['name']];
            $where['brand_id'] = $brand_id;
            $where['type'] = $input['material']['material_type'];
			$where['delete_id'] = 0;
            $material = $this->material_model->getOne($where);
            if(!empty($material)){
                throw new Exception($material_title.'名称已存在。');
            }
            $param = array();
            $param['name'] = $input['material']['name'];
            $param['type'] = $input['material']['material_type'];
            $param['upload_path'] = $input['material']['upload_path'];
            $param['upload_file'] = $input['material']['upload_file'];
            $param['upload_name'] = $input['material']['upload_name'];
            $param['upload_type'] = $input['material']['upload_type'];
            $param['upload_size'] = $input['material']['upload_size'];
            $param['url'] = $input['material']['upload_url'];
            $param['create_time'] = $this->phpdate->time();
            $param['update_time'] = $param['create_time'];
            $param['create_id'] = $this->permis_logic->getLoginUserInfo()['id'];
            $param['update_id'] = $this->permis_logic->getLoginUserInfo()['id'];
            $param['brand_id'] = $brand_id;
            $param['tagname'] = $input['material']['tagname'];//标签
            if(isset($input['material']['thumbnail']))
                $param['thumbnail'] = $input['material']['thumbnail'];//缩略图地址
            $this->material_model->newData($param);
            redirect('admin/material/index');die;
        } catch (Exception $e) {
            $this->assign += $input['material'];
            //获取品牌列表
            if('yes' == $this->assign['is_admin']){
                $this->vendor('@.Brand_model');
                $brands = $this->brand_model->getAll();
                $this->assign['brand_opts'] = $brands;
            }
            $this->assign['material_type'] = $input['material']['material_type'];
            if(0 == $this->assign['material_type']){
                $this->assign['material_title'] = '图片';
            }else{
                $this->assign['material_title'] = '视频';
            }
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/material/create';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function edit($id){
        $material = $this->material_model->getOne(['id'=>$id]);
        unset($material['status']);
        $this->assign += $material;
        $this->assign['id'] = $id;
        //获取品牌列表
        if('yes' == $this->assign['is_admin']){
            $this->vendor('@.Brand_model');
            $brands = $this->brand_model->getAll();
            $this->assign['brand_opts'] = $brands;
        }
        $this->assign['view'] = 'admin/material/edit';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function update($id){
        try {
            $input = $this->input->post();
            if(!isset($input['material'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['material']['name'])){
                throw new Exception('请输入板块标题。');
            }
            $brand_id = 0;
            if(isset($input['material']['brand_id']) && $input['material']['brand_id'] > 0){
                $brand_id = $input['material']['brand_id'];
            }else if('yes' != $this->assign['is_admin']){//非管理员
                $brand_id = $this->permis_logic->getLoginUserInfo()['brand_id'];
            }
            $where = ['name'=>$input['material']['name']];
            if($brand_id > 0){
                $where['brand_id'] = $brand_id;
            }
			$where['delete_id'] = 0;
            $material = $this->material_model->getOne($where);
            if(!empty($material) && $id != $material['id']){
                throw new Exception('板块标题已存在。');
            }
            $param = $input['material'];
            $param['update_time'] = $this->phpdate->time();
            $param['update_id'] = $this->permis_logic->getLoginUserInfo()['id'];
            $this->material_model->updateById($id,$param);
            redirect('admin/material/index');die;
        } catch (Exception $e) {
            $this->assign += $input['material'];
            $this->assign['id'] = $id;
            //获取品牌列表
            if('yes' == $this->assign['is_admin']){
                $this->vendor('@.Brand_model');
                $brands = $this->brand_model->getAll();
                $this->assign['brand_opts'] = $brands;
            }
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/material/edit';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 禁用启用
     * @author Toby.tu 2016-08-31
     */
    public function enable($id){
        try {
            $input = $this->input->get();
            if(!isset($input['status'])){
                throw new Exception('参数不正确。');
            }
            $param = array();
            $param['status'] = ($input['status']==1)?0:1;
            $param['update_id'] = $this->permis_logic->getLoginUserInfo()['id'];
            $param['update_time'] = $this->phpdate->time();
            $this->material_model->updateById($id,$param);
            $this->ajaxReturn('SUCCESS');
        } catch (Exception $e) {
            $this->ajaxReturn($e->getMessage(),$e->getCode());
        }
        //     redirect('admin/material/index');die;
        // } catch (Exception $e) {
        //     redirect('admin/material/index');die;
        // }
    }
    /**
     * 删除
     * @author Toby.tu 2016-08-31
     */
    public function destroy($id){
        if('yes' != $this->permis_logic->isAdmin()){
            die('不是管理员，不允许执行删除操作。');
        }
        $param = array();
        $current_id = $this->permis_logic->getLoginUserInfo()['id'];
        $param['delete_id'] = $current_id;
        $param['delete_time'] = $this->phpdate->time();
        $this->material_model->updateById($id,$param);
        redirect('admin/material/index');die;
    }
    public function upload(){
        $this->load->view('admin/material/upload',$this->assign);
    }
    /**
     * 上传地址
     * @author Toby.tu 2017-03-03
     */
    public function uploadfile(){
        $info = array();
        if (isset($_FILES['uploadFile'])){
            $namearr = explode('.', $_FILES['uploadFile']['name']);
            $mtime = $this->phpdate->mtime();
            $file = date('ymdHis',time()).substr($mtime,-4).rand(1000,9999).'.'.$namearr[1];//毫秒时间戳加随机数
            $path = 'upload/'.$this->phpdate->get('Y/m/d');
            $filepath = config_item('root_path').'/'.$path;
            $this->common->createdir($filepath);
            $config['upload_path']      = $filepath;
            $config['allowed_types']    = '*';
            $config['max_size']         = 1000000;
            $config['file_name']        = $file;

            if($_FILES['uploadFile']['name']){
                $this->load->library('Qiniu');
                $img = $this->qiniu->saveFileByNameAndPath('uploadFile','upload/Product/');
                // $tmp['base_image'] = 'http://phpbasefiles.woaap.com/'.$img['key'];
                $info = array();
                $info['upload_path'] = '';
                $info['upload_file'] = '';
                $info['upload_name'] = $_FILES['uploadFile']['name'];
                $info['upload_type'] = $_FILES['uploadFile']['type'];
                $info['upload_size'] = $_FILES['uploadFile']['size'];
                $info['upload_url'] = 'http://phpbasefiles.woaap.com/'.$img['key'];
                $this->load->view('admin/material/upload',$info);
            }

            // $this->load->library('upload', $config);

            // if ( ! $this->upload->do_upload('uploadFile')){
            //     echo json_encode(array('errorcode' => 1001 , 'errormsg' => $this->upload->display_errors()));
            // }else{
            //     $info = $this->upload->data();
            //     $info['upload_path'] = $filepath;
            //     $info['upload_file'] = $file;
            //     $info['upload_name'] = $_FILES['uploadFile']['name'];
            //     $info['upload_type'] = $_FILES['uploadFile']['type'];
            //     $info['upload_size'] = $_FILES['uploadFile']['size'];
            //     $info['upload_url'] = base_url().$path.'/'.$file;
            //     $this->load->view('admin/material/upload',$info);
            // }
        }
    }
    /**
     * 修改素材所属板块
     * @author Toby.tu 2017-03-06
     */
    public function updateGroup(){
        try {
            $input = $this->input->post();
            if(!isset($input['brandinfo_id']) || !isset($input['material_id'])){
                throw new Exception('参数不正确。');
            }
            $param = array();
            $param['brandinfo_id'] = $input['brandinfo_id'];
            $param['update_id'] = $this->permis_logic->getLoginUserInfo()['id'];
            $param['update_time'] = $this->phpdate->time();
            $material_id_arr = explode(',',$input['material_id']);
            $this->material_model->updateInId($material_id_arr,$param);
            $this->ajaxReturn('SUCCESS');
        } catch (Exception $e) {
            $this->ajaxReturn($e->getMessage(),$e->getCode());
        }
    }
    /**
     * 显示
     * @author Toby.tu 2017-03-06
     */
    public function show($id=0){
        if(!$id) die('参数错误');
    }
}
